package com.lzy.wzy.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.wzy.model.FirstLevelTemplateInformationBean;
import com.lzy.wzy.model.LevelOneTemplateBean;
import com.lzy.wzy.vo.*;
import com.lzy.wzy.result.Result;
import com.lzy.wzy.result.ResponseCode;
import com.lzy.wzy.service.TemplateService;
import com.lzy.wzy.utils.PatternUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping("/craft")
@Slf4j
public class CraftController {


    @Autowired
    private TemplateService templateService;



    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/bindingTemplate", method = RequestMethod.POST)
    public Result<T> bindingTemplate(@NotBlank @RequestParam("productCode") String productCode, @NotBlank @RequestParam("templateId") String templateId) {
        if (PatternUtils.checkDigitId(templateId)) {
            return Result.invalidFormat();
        }
        return templateService.doBindingTemplate(productCode, Integer.parseInt(templateId.trim()));
    }

    //返修工单绑定模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/reworkOrderBindTemplate", method = RequestMethod.POST)
    public Result<T> reworkOrderBindTemplate(@NotBlank @RequestParam("workOrderNumber") String workOrderNumber,@NotBlank @RequestParam("templateId") String templateId){
        if (!workOrderNumber.startsWith("F") || PatternUtils.checkDigitId(templateId)) {
            return Result.invalidFormat();
        }
        return templateService.doReworkOrderBindTemplate(workOrderNumber, Integer.parseInt(templateId));
    }

    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/previewTemplate", method = RequestMethod.GET)
    public Result<PreviewVo> previewTemplate(@NotBlank @RequestParam("templateId") String templateId) {
        if (PatternUtils.checkDigitId(templateId)) {
            return new Result<>(ResponseCode.FAILURE.getCode(), ResponseCode.FAILURE.getMsg(), null);
        }
        return templateService.doPreviewTemplate(Integer.parseInt(templateId));
    }

    //分页获取所有 三级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getAllLeaveThreeTemplate", method = RequestMethod.GET)
    public Result<PageInfo<LevelOneTemplateBean>> getAllLeaveThreeTemplate(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
            , @RequestParam(value = "pageSize", defaultValue = "40") int pageSize) {
        return templateService.doGetAllLeaveThreeTemplate(pageNum, pageSize);
    }

    //分页获取所有 二级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getAllLeaveTwoTemplate", method = RequestMethod.GET)
    public Result<PageInfo<LevelOneTemplateBean>> getAllLeaveTwoTemplate(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
            , @RequestParam(value = "pageSize", defaultValue = "40") int pageSize) {
        return templateService.doGetAllLeaveTwoTemplate(pageNum, pageSize);
    }

    //获取所有 一级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getAllFirstLevelTemplates", method = RequestMethod.GET)
    public Result<List<LevelOneTemplateBean>> getAllFirstLevelTemplates() {
        return templateService.doGetAllFirstLevelTemplates();
    }
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getAllFirstLevelTemplatesPassed", method = RequestMethod.GET)
    public Result<List<LevelOneTemplateBean>> getAllFirstLevelTemplatesPassed(){
        return templateService.doGetAllFirstLevelTemplatesPassed();
    }

    //获取产品编码和模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getAllProductCodesAndTemplates", method = RequestMethod.GET)
    public Result<ProductCodeAndTemplateVo> getAllProductCodesAndTemplates() {
        return templateService.doGetAllProductCodesAndTemplates();
    }
    //获取返修工单和模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/GetAllRepairWorkOrdersAndTemplates", method = RequestMethod.GET)
    public Result<ReworkOrderAndTemplateVo> GetAllRepairWorkOrdersAndTemplates(){
        return templateService.doGetAllRepairWorkOrdersAndTemplates();
    }



    //校验三级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/checkLeaveThreeTemplateName", method = RequestMethod.GET)
    public Result<T> checkLeaveThreeTemplateName(@RequestParam("templateName") String templateName) {
        if (PatternUtils.checkTemplateName(templateName)) {
            return new Result<>(ResponseCode.FAILURE.getCode(), ResponseCode.FAILURE.getMsg(), null);
        }
        return templateService.doCheckLeaveThreeTemplateName(templateName);
    }

    //添加三级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/addLeaveThreeTemplate", method = RequestMethod.POST)
    public Result<T> addLeaveThreeTemplate(@RequestParam("templateName") String templateName,@NotBlank @RequestParam("fieldName") String fieldName) {
        if (PatternUtils.checkTemplateName(templateName) || PatternUtils.checkFieldName(fieldName)) {
            return new Result<>(ResponseCode.FAILURE.getCode(), ResponseCode.FAILURE.getMsg(), null);
        }
        return templateService.doAddLeaveThreeTemplate(templateName, fieldName);
    }

    //添加二级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/addLeaveTwoTemplate", method = RequestMethod.POST)
    public Result<T> addLeaveTwoTemplate(@Validated @RequestBody UpFirstLevelTemplateVo upFirstLevelTemplateVo) {
        return templateService.doAddLeaveTwoTemplate(upFirstLevelTemplateVo);
    }

    //添加一级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/addLeaveOneTemplate", method = RequestMethod.POST)
    public Result<T> addLeaveOneTemplate(@Validated @RequestBody UpFirstLevelTemplateVo upFirstLevelTemplateVo) {
        return templateService.doAddLeaveOneTemplate(upFirstLevelTemplateVo);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/modifyProductAndTemplateBindingInformation", method = RequestMethod.POST)
    public Result<T> modifyProductAndTemplateBindingInformation(@NotBlank @RequestParam("productCode") String productCode,@NotBlank @RequestParam("newTemplateId") String newTemplateId,@NotBlank @RequestParam("oldTemplateId") String oldTemplateId){
        if (PatternUtils.checkDigitId(newTemplateId) || PatternUtils.checkDigitId(oldTemplateId)) {
            return Result.invalidFormat();
        }
        if (newTemplateId.equals(oldTemplateId)) {
            return new Result<>(ResponseCode.FAILURE.getCode(), "新模板不能和旧模板一样", null);
        }
        return templateService.doModifyProductAndTemplateBindingInformation(productCode,Integer.parseInt(newTemplateId),Integer.parseInt(oldTemplateId));
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRODUCE','ROLE_QUALITY','QUALITY_INSPECTION','QUALITY_INSPECTION','ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/viewTestDataModificationRecords/{workOrderNumber}/{templateId}", method = RequestMethod.GET)
    public Result<ModifyRecordVo> viewTestDataModificationRecords(@NotBlank @PathVariable("workOrderNumber") String workOrderNumber , @NotBlank @PathVariable("templateId") String templateId){
        if (PatternUtils.checkDigitId(templateId) ||!PatternUtils.checkWorkOrderNumber(workOrderNumber)) {
            return Result.invalidFormat();
        }
        return templateService.doViewTestDataModificationRecords(workOrderNumber, Integer.parseInt(templateId));
    }

    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getTheCorrespondingTemplateOfTheProductCode", method = RequestMethod.GET)
    public Result<List<LevelOneTemplateBean>> getTheCorrespondingTemplateOfTheProductCode(@NotBlank @RequestParam("productCode") String productCode){
        return templateService.doGetTheCorrespondingTemplateOfTheProductCode(productCode);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT_DIRECTOR','ROLE_ADMIN')")
    @RequestMapping(value = "/deleteTemplate", method = RequestMethod.GET)
    public Result<T> deleteTemplate(@NotBlank @RequestParam("templateId") String templateId){
        if (PatternUtils.checkDigitId(templateId)){
            return Result.invalidFormat();
        }
        return templateService.doDeleteTemplate(Integer.parseInt(templateId));
    }

    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT_DIRECTOR','ROLE_ADMIN')")
    @RequestMapping(value = "/deleteTableData", method = RequestMethod.GET)
    public Result<T> deleteTableData(@NotBlank @RequestParam("templateId") String templateId){
        if (PatternUtils.checkDigitId(templateId)){
            return Result.invalidFormat();
        }
        return templateService.doDeleteDataTable(Integer.parseInt(templateId));
    }

    //工艺主管获取模板信息附上状态
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT_DIRECTOR','ROLE_ADMIN')")
    @RequestMapping(value = "/getAllFirstLevelTemplateInformation", method = RequestMethod.GET)
    public Result<PageInfo<FirstLevelTemplateInformationBean>> getAllFirstLevelTemplateInformation(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
            , @RequestParam(value = "pageSize", defaultValue = "20") int pageSize){
        return templateService.doGetAllFirstLevelTemplateInformation(pageNum,pageSize);
    }

    //批准审核通过
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT_DIRECTOR','ROLE_ADMIN')")
    @RequestMapping(value = "/approved", method = RequestMethod.GET)
    public Result<String> approved(@NotBlank @RequestParam("templateId") String templateId , @NotBlank @RequestParam("templateName") String templateName){
        if (PatternUtils.checkDigitId(templateId)){
            return Result.invalidFormat();
        }
        return templateService.doUpdateFirstLevelTemplateStatus(Integer.parseInt(templateId),templateName);
    }

    //获取二级及以下子模板信息
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getChildTemplateInformation", method = RequestMethod.GET)
    public Result<ChildTemplateVo> getChildTemplateInformation(@NotBlank @RequestParam("templateId") String templateId){
        if (PatternUtils.checkDigitId(templateId)){
            return Result.invalidFormat();
        }
        return templateService.dogGetChildTemplateInformation(Integer.parseInt(templateId));
    }

    //删除三级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/deleteLevelThreeTemplate", method = RequestMethod.GET)
    public Result<T> deleteLevelThreeTemplate(@NotBlank @RequestParam("templateId") String templateId ,@NotBlank @RequestParam("reason") String reason){
        if (PatternUtils.checkDigitId(templateId)){
            return Result.invalidFormat();
        }
        return templateService.doDeleteLevelThreeTemplate(Integer.parseInt(templateId),reason);
    }

    //删除二级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/deleteLevelTwoTemplate", method = RequestMethod.GET)
    public Result<T> deleteLevelTwoTemplate(@NotBlank @RequestParam("templateId") String templateId,@NotBlank @RequestParam("reason") String reason){
        if (PatternUtils.checkDigitId(templateId)){
            return Result.invalidFormat();
        }
        return templateService.doDeleteLevelTwoTemplate(Integer.parseInt(templateId),reason);
    }

    //获取所有未被使用过的三级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getUnusedThreeLevelTemplates", method = RequestMethod.GET)
    public Result<List<LevelOneTemplateBean>> getUnusedThreeLevelTemplates(){
        return templateService.doGetUnusedThreeLevelTemplates();
    }

    //获取所有未被使用过的二级模板
    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getUnusedTwoLevelTemplates", method = RequestMethod.GET)
    public Result<List<LevelOneTemplateBean>> getUnusedTwoLevelTemplates(){
        return templateService.doGetUnusedTwoLevelTemplates();
    }

    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT_DIRECTOR','ROLE_PROCESS_ENGINEER','ROLE_ADMIN')")
    @RequestMapping(value = "/contactBinding", method = RequestMethod.GET)
    public Result<T> removeProductCodingAndTemplateMapping(@NotBlank @RequestParam("productCode") String productCode,@NotBlank @RequestParam("templateId") String templateId){
        if (PatternUtils.checkDigitId(templateId)){
            return Result.invalidFormat();
        }
        return templateService.doRemoveProductCodingAndTemplateMapping(productCode,Integer.parseInt(templateId));
    }

    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/updateTheRepairWorkOrderTemplate/{workOrderNumber}/{newTemplateId}/{oldTemplateId}", method = RequestMethod.GET)
    public Result<T> updateTheRepairWorkOrderTemplate(@NotBlank @PathVariable("workOrderNumber")String workOrderNumber,@PathVariable("newTemplateId") int newTemplateId,
                                                        @PathVariable("oldTemplateId")int oldTemplateId){
        return templateService.doUpdateTheRepairWorkOrderTemplate(workOrderNumber,newTemplateId,oldTemplateId);
    }


    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getTemplateInfoByRework/{workOrderNumber}", method = RequestMethod.GET)
    public Result<LevelOneTemplateBean> getTemplateInfoByRework(@NotBlank @PathVariable("workOrderNumber")String workOrderNumber){
        if (!PatternUtils.checkWorkOrderNumber(workOrderNumber)){
            return new Result<>(ResponseCode.WRONG_FORMAT.getCode(), ResponseCode.WRONG_FORMAT.getMsg(),null);
        }
        return templateService.doGetTemplateInfoByRework(workOrderNumber);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/getAllReworkOrderIsBinding", method = RequestMethod.GET)
    public Result<ReworkOrderAndTemplateVo> getAllReworkOrderIsBinding(){
        return templateService.doGetAllReworkOrderIsBinding();
    }

    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT_DIRECTOR','ROLE_ADMIN')")
    @RequestMapping(value = "/disabledTemplate/{templateId}")
    public Result<String> disabledTemplate(@PathVariable("templateId") int templateId){
        if (PatternUtils.checkDigitId(String.valueOf(templateId))){
            return Result.invalidFormat();
        }
        return templateService.doDisabledTemplate(templateId);
    }


    @PreAuthorize("hasAnyAuthority('ROLE_CRAFT_DIRECTOR','ROLE_ADMIN')")
    @RequestMapping(value = "/enabledTemplate/{templateId}")
    public Result<String> enabledTemplate(@PathVariable("templateId") int templateId){
        if (PatternUtils.checkDigitId(String.valueOf(templateId))){
            return Result.invalidFormat();
        }
        return templateService.doEnabledTemplate(templateId);
    }
}
